package com.haierCamera.customapplication.ui.main;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.CompoundButton;

import com.haierCamera.customapplication.BaseActivity;
import com.haierCamera.customapplication.R;
import com.haierCamera.customapplication.api.ApiService;
import com.haierCamera.customapplication.databinding.HzklActivityMsgAlarmBinding;
import com.haierCamera.customapplication.ui.business.Business;
import com.haierCamera.customapplication.utils.ResourceConvertUtils;
import com.haierCamera.customapplication.utils.SharedPreferencesUtils;
import com.haierCamera.customapplication.utils.android.HttpErrorProcess;

import javax.inject.Inject;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import dagger.Lazy;

public class HZKLMsgAlarmSettingActivity extends BaseActivity {

    HzklActivityMsgAlarmBinding binding;

    @Inject
    Lazy<HttpErrorProcess> httpErrorProcessLazy;
    @Inject
    ApiService apiService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = DataBindingUtil.setContentView(this, R.layout.hzkl_activity_msg_alarm);

        binding.imgReturn.setOnClickListener((v)->{
            finish();
        });
        binding.viewSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (compoundButton.isPressed()) {
                    Log.i("TAG","isPressed-" + b);
                    if (b) {
                        setAlarmSwitch(getIntent().getStringExtra("devId"),1);
                    } else {
                        setAlarmSwitch(getIntent().getStringExtra("devId"),0);
                    }
                }
            }
        });

        init();
    }

    private void init(){

        getAlarmSwitch(getIntent().getStringExtra("devId"));
    }

    public void getAlarmSwitch(String sn) {
        showLoadingDialog();
        ResourceConvertUtils.convertToResource(apiService.getAlarmSwitch(sn))
                .observe(this, resource -> {
                    assert resource != null;
                    switch (resource.status) {
                        case ERROR:
                            httpErrorProcessLazy.get().process(resource);
                            dismissLoadingDialog();
                            break;
                        case SUCCESS:
                            dismissLoadingDialog();
                            Log.i("TAG","obj-" + resource.data.switchValue);
                            if (resource.data.switchValue == 0) {
                                binding.viewSwitch.setChecked(false);
                            } else if (resource.data.switchValue == 1) {
                                binding.viewSwitch.setChecked(true);
                            }


                            break;
                        case LOADING:
                            break;

                    }
                });
    }

    public void setAlarmSwitch(String sn,int swi) {
        showLoadingDialog();
        ResourceConvertUtils.convertToResource(apiService.setAlarmSwitch(sn,swi))
                .observe(this, resource -> {
                    assert resource != null;
                    switch (resource.status) {
                        case ERROR:
                            httpErrorProcessLazy.get().process(resource);
                            dismissLoadingDialog();
                            if (swi == 1) {
                                binding.viewSwitch.setChecked(false);
                            } else if (swi == 0) {
                                binding.viewSwitch.setChecked(true);
                            }
                            break;
                        case SUCCESS:
                            dismissLoadingDialog();

                            break;
                        case LOADING:
                            break;

                    }
                });
    }
}
